/*
 * Copyright (c) 2022, CCSSOFT All Rights Reserved.
 */
package net.sf.log4jdbc.http.handler;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import net.sf.log4jdbc.http.HttpUtils;

/**
 * 参数配置
 * @author zhanxb
 * @since 2022/6/20
 **/
public class SettingHttpHandler implements HttpHandler
{
    Map<String, String> sessionMap = new HashMap<String, String>();

    @Override
    public void handle(HttpExchange httpExchange) throws IOException
    {
        String queryString = httpExchange.getRequestURI().getQuery();
        Map<String, String> map = HttpUtils.formData2Map(queryString);
        String qinpkgs = map.get("q_inpkgs");
        String qexpkgs = map.get("q_expkgs");
        String qshowDetail = map.get("q_showDetail");
        String qonlyHttp = map.get("q_onlyHttp");
        if (this.sessionMap.get("inpkgsHis") == null || "".equals(this.sessionMap.get("inpkgsHis")))
        {
            this.sessionMap.put("inpkgsHis", System.getProperties().getProperty("inpkgs", ""));
        }
        if (this.sessionMap.get("expkgsHis") == null || "".equals(this.sessionMap.get("expkgsHis")))
        {
            this.sessionMap.put("expkgsHis", System.getProperties().getProperty("expkgs", ""));
        }
        if (qinpkgs != null && !"".equals(qinpkgs))
        {
            if (!qinpkgs.equals(System.getProperties().getProperty("inpkgs", "").equals("") ? "blank"
                    : System.getProperties().getProperty("inpkgs", "")))
            {
                if ("".equals(this.sessionMap.get("inpkgsHis")))
                {
                    this.sessionMap.put("inpkgsHis", qinpkgs);
                } else
                {
                    this.sessionMap.put("inpkgsHis", this.sessionMap.get("inpkgsHis") + "<br>" + qinpkgs);
                }
            }
            if ("blank".equals(qinpkgs))
            {
                System.setProperty("inpkgs", "");
            } else
            {
                System.setProperty("inpkgs", qinpkgs);
            }
        }
        if (qexpkgs != null && !"".equals(qexpkgs))
        {
            if (!qexpkgs.equals(System.getProperties().getProperty("expkgs", "").equals("") ? "blank"
                    : System.getProperties().getProperty("expkgs", "")))
            {
                if ("".equals(this.sessionMap.get("expkgsHis")))
                {
                    this.sessionMap.put("expkgsHis", qexpkgs);
                } else
                {
                    this.sessionMap.put("expkgsHis", this.sessionMap.get("expkgsHis") + "<br>" + qexpkgs);
                }

            }
            if ("blank".equals(qexpkgs))
            {
                System.setProperty("expkgs", "");
            } else
            {
                System.setProperty("expkgs", qexpkgs);
            }
        }

        if (qshowDetail != null && !"".equals(qshowDetail))
        {
            System.setProperty("showDetail", qshowDetail);
        }
        if (qonlyHttp != null && !"".equals(qonlyHttp))
        {
            System.setProperty("onlyHttpInvoke", qonlyHttp);
        }
        String inpkgs = System.getProperties().getProperty("inpkgs", "");
        String expkgs = System.getProperties().getProperty("expkgs", "");
        String showDetail = System.getProperties().getProperty("showDetail", "");
        String onlyHttp = System.getProperties().getProperty("onlyHttpInvoke", "");

        Map<String, String> params = new HashMap<String, String>();
        params.put("inpkgs", inpkgs);
        params.put("expkgs", expkgs);
        params.put("showDetail", showDetail);
        params.put("onlyHttp", onlyHttp);
        params.put("inpkgsBr", inpkgs.replace(";", "<br>"));
        params.put("expkgsBr", expkgs.replace(";", "<br>"));
        params.put("inpkgsHis", this.sessionMap.get("inpkgsHis"));
        params.put("expkgsHis", this.sessionMap.get("expkgsHis"));

        httpExchange.sendResponseHeaders(200, 0);
        OutputStream os = httpExchange.getResponseBody();
        os.write(this.toHtml(params).getBytes(Charset.forName("UTF-8")));
        os.close();
    }

    private String toHtml(Map<String, String> params)
    {
        StringBuilder htmlBuffer = new StringBuilder();
        htmlBuffer.append("<html>").append("\n");
        htmlBuffer.append("<head>").append("\n");
        htmlBuffer.append("    <META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html;charset=utf-8\">").append("\n");
        htmlBuffer.append("    <META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\"/>").append("\n");
        htmlBuffer.append("    <META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">").append("\n");
        htmlBuffer.append("    <META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">").append("\n");
        htmlBuffer.append("    <title>参数设置</title>").append("\n");
        htmlBuffer.append("    <script type=\"text/javascript\">").append("\n");
        htmlBuffer.append("        function init() {").append("\n");
        htmlBuffer.append("            var showDetail = \"").append(params.get("showDetail")).append("\";\n");
        htmlBuffer.append("            showDetail = showDetail.toUpperCase();").append("\n");
        htmlBuffer.append("            if(\"Y\"==showDetail||\"TRUE\"==showDetail||\"1\"==showDetail){").append("\n");
        htmlBuffer.append("                document.getElementById(\"showDetailTrue\").checked=true;").append("\n");
        htmlBuffer.append("            }else {").append("\n");
        htmlBuffer.append("                document.getElementById(\"showDetailFalse\").checked=true;").append("\n");
        htmlBuffer.append("            }").append("\n");
        htmlBuffer.append("            var onlyHttp = \"").append(params.get("onlyHttp")).append("\";\n");
        htmlBuffer.append("            onlyHttp = onlyHttp.toUpperCase();").append("\n");
        htmlBuffer.append("            if(\"Y\"==onlyHttp||\"TRUE\"==onlyHttp||\"1\"==onlyHttp){").append("\n");
        htmlBuffer.append("                document.getElementById(\"onlyHttpTrue\").checked=true;").append("\n");
        htmlBuffer.append("            }else {").append("\n");
        htmlBuffer.append("                document.getElementById(\"onlyHttpFalse\").checked=true;").append("\n");
        htmlBuffer.append("            }").append("\n");
        htmlBuffer.append("        }").append("\n");
        htmlBuffer.append("    </script>").append("\n");
        htmlBuffer.append("</head>").append("\n");
        htmlBuffer.append("<body onload=\"init()\">").append("\n");
        htmlBuffer.append("<form action=\"/setting\" method=\"get\">").append("\n");
        htmlBuffer.append("    <table>").append("\n");
        htmlBuffer.append("        <tr>").append("\n");
        htmlBuffer.append("            <td><label>inpkgs参数:</label></td>").append("\n");
        htmlBuffer
                .append("            <td colspan=\"3\"><input type=\"text\" id=\"q_inpkgs\" name=\"q_inpkgs\" value=\"")
                .append(params.get("inpkgs")).append("\" style=\"width: 1200px\">").append("\n");
        htmlBuffer.append("            </td>").append("\n");
        htmlBuffer.append("        </tr>").append("\n");
        htmlBuffer.append("        <tr>").append("\n");
        htmlBuffer.append("            <td><label>expkgs参数:</label></td>").append("\n");
        htmlBuffer
                .append("            <td colspan=\"3\"><input type=\"text\" id=\"q_expkgs\" name=\"q_expkgs\" value=\"")
                .append(params.get("expkgs")).append("\" style=\"width: 1200px\">").append("\n");
        htmlBuffer.append("            </td>").append("\n");
        htmlBuffer.append("        </tr>").append("\n");
        htmlBuffer.append("        <tr>").append("\n");
        htmlBuffer.append("            <td><label>是否显示详情:</label></td>").append("\n");
        htmlBuffer.append("            <td colspan=\"3\">").append("\n");
        htmlBuffer.append(
                "                <input type=\"radio\" id=\"showDetailTrue\" name=\"q_showDetail\" value=\"true\" />是")
                .append("\n");
        htmlBuffer.append(
                "                <input type=\"radio\" id=\"showDetailFalse\" name=\"q_showDetail\" value=\"false\"/>否")
                .append("\n");
        htmlBuffer.append("            </td>").append("\n");
        htmlBuffer.append("        </tr>").append("\n");
        htmlBuffer.append("        <tr>").append("\n");
        htmlBuffer.append("            <td><label>只输出HTTP调用:</label></td>").append("\n");
        htmlBuffer.append("            <td colspan=\"3\">").append("\n");
        htmlBuffer.append(
                "                <input type=\"radio\" id=\"onlyHttpTrue\" name=\"q_onlyHttp\" value=\"true\" />是")
                .append("\n");
        htmlBuffer.append(
                "                <input type=\"radio\" id=\"onlyHttpFalse\" name=\"q_onlyHttp\" value=\"false\"/>否")
                .append("\n");
        htmlBuffer.append("            </td>").append("\n");
        htmlBuffer.append("        </tr>").append("\n");
        htmlBuffer.append("        <tr>").append("\n");
        htmlBuffer.append("            <td></td>").append("\n");
        htmlBuffer.append("            <td></td>").append("\n");
        htmlBuffer.append("            <td></td>").append("\n");
        htmlBuffer.append("            <td><input type=\"submit\" value=\"提交\"></td>").append("\n");
        htmlBuffer.append("        </tr>").append("\n");
        htmlBuffer.append("    </table>").append("\n");
        htmlBuffer.append("</form>").append("\n");
        htmlBuffer.append("<b>【参数值】</b><br>").append("\n");
        htmlBuffer.append("<table border=\"1\">").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <th>参数名</th>").append("\n");
        htmlBuffer.append("        <th>参数值</th>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>inpkgs:</td>").append("\n");
        htmlBuffer.append("        <td>").append(params.get("inpkgsBr")).append("</td>\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>expkgs:</td>").append("\n");
        htmlBuffer.append("        <td>").append(params.get("expkgsBr")).append("</td>\n");
        htmlBuffer.append("        </td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>showDetail:</td>").append("\n");
        htmlBuffer.append("        <td>").append(params.get("showDetail")).append("</td>\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>onlyHttp:</td>").append("\n");
        htmlBuffer.append("        <td>").append(params.get("onlyHttp")).append("</td>\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("</table>").append("\n");
        htmlBuffer.append("<br>").append("\n");
        htmlBuffer.append("<br>").append("\n");
        htmlBuffer.append("<b>【参考说明】</b><br>").append("\n");
        htmlBuffer.append("<table border=\"1\">").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer
                .append("        <td colspan=\"2\"><b>inpkgs:</b>该参数是设置全类名匹配的表达式，只有调用堆栈链里匹配到一个及以上的结果就表示打印堆栈痕迹和SQL<br>")
                .append("\n");
        htmlBuffer.append("            <b>expkgs:</b>该参数是设置全类名匹配的表达式，只要是调用堆栈链里匹配到一个结果就不打印打印堆栈痕迹和SQL<br>").append("\n");
        htmlBuffer.append("            <b>showDetail:</b>是否显示每个SQL动态参数对应的是JavaType，执行的set方法等").append("\n");
        htmlBuffer.append("        </td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <th>包名通配符</th>").append("\n");
        htmlBuffer.append("        <th>通配符说明</th>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>com.aaa.idc.*;</td>").append("\n");
        htmlBuffer.append("        <td>匹配包com.aaa.idc下的所有类;</td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>com.aaa.idc.rs.*.dao.*;</td>").append("\n");
        htmlBuffer.append("        <td>匹配包com.aaa.idc.rs下所有dao子目录的类;</td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>com.aaa.ida40.ifap.*.*Msg*;</td>").append("\n");
        htmlBuffer.append("        <td>匹配包com.aaa.ida40.ifap下所有类名或方法名包含Msg的类和方法;</td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>com.aaa.ida40.ifap.*.*Filter;</td>").append("\n");
        htmlBuffer.append("        <td>匹配包com.aaa.ida40.ifap下所有类名有Filter;</td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>!com.aaa.idc.*;</td>").append("\n");
        htmlBuffer.append("        <td>不匹配包com.aaa.idc下的所有类;</td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>*;</td>").append("\n");
        htmlBuffer.append("        <td>匹配所有类和方法。</td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("    <tr>").append("\n");
        htmlBuffer.append("        <td>blank;</td>").append("\n");
        htmlBuffer.append("        <td>设置参数为空值。匹配空值。</td>").append("\n");
        htmlBuffer.append("    </tr>").append("\n");
        htmlBuffer.append("</table>").append("\n");
        htmlBuffer.append("<br><br><br>").append("\n");
        htmlBuffer.append("<label><b>inpkgs参数痕迹记录：</b></label><br>").append("\n");
        htmlBuffer.append("<label>").append(params.get("inpkgsHis")).append("</label><br>").append("\n");
        htmlBuffer.append("<label><b>expkgs参数痕迹记录：</b></label><br>").append("\n");
        htmlBuffer.append("<label>").append(params.get("expkgsHis")).append("</label><br>").append("\n");
        htmlBuffer.append("</body>").append("\n");
        htmlBuffer.append("</html>").append("\n");
        return htmlBuffer.toString();
    }
}
